package cn.lzy.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * @ClassName QueueProducer
 * @Author liu zhiyong
 * @Date 2020/8/13 14:58
 * @Version 1.0
 */
@Component
public class QueueProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Destination queueTextDestination;

    @Autowired
    private Destination topicTextDestination;

    /**
     * 发送文本消息-点对点模式
     * @Author liu zhiyong
     * @Date 2020/8/13 15:00
     * @Version 1.0
     * @param text
     * @return void
     **/
    public void sendTextMessage(final String text){

        jmsTemplate.send(queueTextDestination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(text);
            }
        });
    }

    /**
     * 发送文本消息-发布订阅模式
     * @Author liu zhiyong
     * @Date 2020/8/13 15:00
     * @Version 1.0
     * @param text
     * @return void
     **/
    public void sendTextMessage2(final String text){

        jmsTemplate.send(topicTextDestination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(text);
            }
        });
    }
}
